package com.base.myproject.client.ui;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

/**
 * 图片上传，图片上传里面的参数至少要有
 * table  表名
 * key	主键
 * keyvalue 主键值
 * idx	主键名称
 * bcode 上传人员代码
 * @author zgw
 *2009-6-19 下午04:37:03
 * javazgw@gmail.com
 */
public class BaseImageDialog extends Dialog {
	String param = null;
	public BaseImageDialog(String param) {
		super();
		this.param = param;
		
		init();
	}

	public void init() {
		ContentPanel panel = new ContentPanel();
		panel.setLayout(new RowLayout(Orientation.HORIZONTAL));
		// panel.setHeaderVisible(false);
		Image i = new Image();
		i.setUrl("images/test/photos/1.jpg");
		panel.add(i, new RowData(.4, 0.8));
		HTML html = new HTML("");
		panel.add(html, new RowData(0.2, 0.8));
		final Image i2 = new Image();
		i2.setUrl("file:///d:\\temp\\2.jpg");
		// i2.setVisible(false);
		panel.add(i2, new RowData(0.4, 0.8));
		// this.setFrame(false);
		// ---

		final FormPanel form = new FormPanel();
		form.setLayout(new RowLayout(Orientation.HORIZONTAL));
		form.setEncoding(Encoding.MULTIPART);
		form.setMethod(Method.POST);
		form.setHeaderVisible(false);
		
		// HorizontalPanel holder = new HorizontalPanel();
		final FileUploadField file = new FileUploadField() {
			protected void onChange(ComponentEvent ce) {
				setValue(getFileInput().getValue());
				i2.setUrl("file:///" + getFileInput().getValue());
			}

		};

		file.setAllowBlank(false);
		file.setName("upload");
		
		form.add(file);
		file.setWidth(400);
		//form.setHeight(80);
		// form.add(new HTML("<hr />"));

		// holder.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
		Button button = new Button("上传");
		/*
		 * file.addListener(Event.ONCHANGE, new Listener<BaseEvent> (){
		 * 
		 * @Override public void handleEvent(BaseEvent be) { // TODO
		 * Auto-generated method stub
		 * System.out.println("------"+file.getValue());
		 * i2.setUrl("file:///"+file.getValue()); }});
		 */
//		form.mask(".......");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				if (!form.isValid()) {
					return;
				}
				form.submit();
			
			}

		});

		// form.add(holder);
		form.add(button);
	//	form.setAction("/gwtbase/BaseFileUploadServlet"); //?"+param==null?"":param.
		System.out.println("/"+GWT.getModuleName()+"/BaseFileUploadServlet?"+(param==null?"":param));
		form.setAction("/"+GWT.getModuleName()+"/BaseFileUploadServlet?"+(param==null?"":param));
		
		// ---end
		
		panel.setHeaderVisible(false);
		this.setActive(true);
		this.setClosable(true);
		this.setLayout(new FitLayout());
		
		this.setSize(500, 200);
	//	this.setScrollMode(Scroll.AUTO);
		panel.setSize(400, 250);
		
	//	panel.setHeight("100%");
	//	this.add(panel);
		 this.add(form);
	//	this.addButton(new Button("按钮"));
		
		// this.add(form,new RowData(1, 0.2));
		setHideOnButtonClick(true);
	}
	// protected void onRender(Element parent, int pos) {
	//		 
	// }

}
